var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        bulletShow: !1,
        money: "",
        host: "",
        uniacid: "",
        wx_token: "",
        session: "",
        dispatch_type_id: "",
        goods_id: "",
        store_id: "",
        wait_adverts: [],
        checkLogin: !1,
        CodePayBol: !0
    },
    onLoad: function(t) {
        wx.setNavigationBarColor({
            backgroundColor: "#24ab35",
            frontColor: "#000000",
            animation: {
                duration: 400,
                timingFunc: "easeIn"
            }
        }), wxfaceapp.launchMp({
            appId: a.data.front_accept_message_id,
            hostAppId: a.data.front_mini_programs_id,
            miniappType: 2,
            launchPage: "packageC/frogpro/backScreenlogin/backScreenlogin",
            needLogin: 0,
            success: function(t) {},
            fail: function(t) {}
        });
        var e = this;
        wxfaceapp.onRemoteMessage(function(t) {
            if (t.content) {
                var o = JSON.parse(t.content);
                o.moneyBol && (o.bolshow ? (e.setData({
                    bulletShow: !0,
                    money: o.money
                }), wx.setNavigationBarColor({
                    backgroundColor: "#fff",
                    frontColor: "#000000",
                    animation: {
                        duration: 400,
                        timingFunc: "easeIn"
                    }
                })) : (e.setData({
                    bulletShow: !1
                }), wx.setNavigationBarColor({
                    backgroundColor: "#24ab35",
                    frontColor: "#000000",
                    animation: {
                        duration: 400,
                        timingFunc: "easeIn"
                    }
                }))), o.showBol && wxfaceapp.launchMp({
                    appId: a.data.front_accept_message_id,
                    hostAppId: a.data.front_mini_programs_id,
                    miniappType: 2,
                    launchPage: "packageC/frogpro/backScreenlogin/backScreenlogin",
                    needLogin: 0,
                    success: function(t) {},
                    fail: function(t) {}
                }), o.wx_token && e.setData({
                    session: o.session,
                    wx_token: o.wx_token,
                    store_id: o.store_id,
                    dispatch_type_id: o.dispatch_type_id,
                    goods_id: o.goods_id,
                    host: o.host,
                    uniacid: o.uniacid
                });
            }
        }), wxfaceapp.listenCodePayment({
            success: function(t) {
                wxfaceapp.onCodePayEvent(function(t) {
                    if (e.data.bulletShow) if (e.data.CodePayBol) {
                        var a = t.code, o = a.slice(0, 2);
                        o >= 10 && o <= 15 ? e.paybtn(a, "Wechat") : o >= 25 && o <= 30 && e.paybtn(a, "walipay");
                    } else wx.showToast({
                        title: "正在支付中,请勿重复扫码",
                        icon: "none",
                        duration: 3e3
                    }); else wx.showToast({
                        title: "等待输入金额",
                        icon: "none",
                        duration: 3e3
                    });
                });
            }
        });
    },
    getNetAddresss: function(t) {
        return this.data.host + "/addons/yun_shop/api.php?i=" + this.data.uniacid + "&session_id=" + this.data.wx_token + "&type=12&route=" + t;
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    paybtn: function(t, e) {
        var o = this, n = "", i = "";
        "Wechat" == e ? (n = "plugin.store-cashier.frontend.pos.merge-pay.wechat-scan-pay", 
        i = o.data.money ? 100 * o.data.money : 0) : "walipay" == e && (n = "plugin.store-cashier.frontend.pos.merge-pay.walipay-scan-pay", 
        i = o.data.money ? o.data.money : 0), t && (o.setData({
            CodePayBol: !1
        }), wx.showLoading({
            title: "正在支付中..."
        }), wx.request({
            url: o.getNetAddresss(n),
            data: {
                auth_code: t,
                amount: i,
                store_id: o.data.store_id,
                dispatch_type_id: o.data.dispatch_type_id,
                goods: JSON.stringify([ {
                    goods_id: o.data.goods_id,
                    total: 1
                } ])
            },
            header: {
                "Content-Type": "application/json",
                Accept: "application/json",
                Cookie: "PHPSESSID=" + o.data.wx_token
            },
            method: "post",
            success: function(t) {
                var e = t.data;
                console.log(e, "res"), 1 == e.result ? (o.setData({
                    bulletShow: !1
                }), wx.setNavigationBarColor({
                    backgroundColor: "#24ab35",
                    frontColor: "#000000",
                    animation: {
                        duration: 400,
                        timingFunc: "easeIn"
                    }
                }), wxfaceapp.launchMp({
                    appId: a.data.front_accept_message_id,
                    hostAppId: a.data.front_mini_programs_id,
                    miniappType: 2,
                    launchPage: "packageC/frogpro/succeededPayment/succeededPayment",
                    needLogin: 0,
                    success: function(t) {
                        wxfaceapp.postMsg({
                            targetAppid: a.data.front_accept_message_id,
                            content: JSON.stringify({
                                money: o.data.money ? o.data.money : "",
                                redirectType: "succeededPayment"
                            }),
                            success: function(t) {},
                            fail: function(t) {}
                        });
                    },
                    fail: function(t) {
                        wx.showModal({
                            title: "提示",
                            content: t,
                            showCancel: !1,
                            success: function(t) {}
                        });
                    }
                }), wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                })) : 0 == e.result && "请登录" === e.msg ? (wxfaceapp.launchMp({
                    appId: a.data.front_accept_message_id,
                    hostAppId: a.data.front_mini_programs_id,
                    miniappType: 2,
                    launchPage: "packageC/frogpro/backScreenlogin/backScreenlogin",
                    needLogin: 0,
                    success: function(t) {
                        wxfaceapp.postMsg({
                            targetAppid: a.data.front_accept_message_id,
                            content: JSON.stringify({
                                redirectType: "login"
                            }),
                            success: function(t) {},
                            fail: function(t) {}
                        });
                    },
                    fail: function(t) {
                        wx.showModal({
                            title: "提示",
                            content: t,
                            showCancel: !1,
                            success: function(t) {}
                        });
                    }
                }), wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                })) : (wxfaceapp.launchMp({
                    appId: a.data.front_accept_message_id,
                    hostAppId: a.data.front_mini_programs_id,
                    miniappType: 2,
                    launchPage: "packageC/frogpro/lose/lose",
                    needLogin: 0,
                    success: function(t) {
                        wxfaceapp.postMsg({
                            targetAppid: a.data.front_accept_message_id,
                            content: JSON.stringify({
                                money: o.data.money ? o.data.money : "",
                                redirectType: "lose"
                            }),
                            success: function(t) {},
                            fail: function(t) {}
                        });
                    },
                    fail: function(t) {
                        wx.showModal({
                            title: "提示",
                            content: t,
                            showCancel: !1,
                            success: function(t) {}
                        });
                    }
                }), wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                })), wx.hideLoading(), o.setData({
                    CodePayBol: !0
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: t,
                    icon: "none",
                    duration: 1500
                }), o.setData({
                    CodePayBol: !0
                });
            }
        }));
    },
    onShareAppMessage: function() {},
    facePayment: function() {
        var t = this;
        wxfaceapp.facePay({
            requireFaceCode: !0,
            success: function(e) {
                wxfaceapp.onFacePayPassEvent(function(a) {
                    var e = a.faceCode;
                    e ? wx.request({
                        url: t.getNetAddresss("plugin.store-cashier.frontend.pos.merge-pay.wechat-scan-face-pay"),
                        data: {
                            auth_code: e,
                            amount: t.data.money ? 100 * t.data.money : 0,
                            store_id: t.data.store_id,
                            dispatch_type_id: t.data.dispatch_type_id,
                            goods: JSON.stringify([ {
                                goods_id: t.data.goods_id,
                                total: 1
                            } ])
                        },
                        header: {
                            "Content-Type": "application/json",
                            Accept: "application/json",
                            Cookie: "PHPSESSID=" + t.data.wx_token
                        },
                        method: "post",
                        success: function(a) {
                            var e = a.data;
                            1 == e.result ? wx.showToast({
                                title: e.msg,
                                icon: "none",
                                duration: 1500
                            }) : 0 == e.result && "请登录" === e.msg ? (wx.showToast({
                                title: e.msg,
                                icon: "none",
                                duration: 1500
                            }), t.data.checkLogin = !0) : wx.showToast({
                                title: e.msg,
                                icon: "none",
                                duration: 1500
                            });
                        },
                        fail: function(t) {
                            wx.showToast({
                                title: t,
                                icon: "none",
                                duration: 1500
                            });
                        }
                    }) : wx.showToast({
                        title: a,
                        icon: "none",
                        duration: 3e3
                    });
                }), wxfaceapp.onQueryPaymentSucEvent(function(e) {
                    t.setData({
                        bulletShow: !1
                    }), wx.setNavigationBarColor({
                        backgroundColor: "#24ab35",
                        frontColor: "#000000",
                        animation: {
                            duration: 400,
                            timingFunc: "easeIn"
                        }
                    }), wxfaceapp.launchMp({
                        appId: a.data.front_accept_message_id,
                        hostAppId: a.data.front_mini_programs_id,
                        miniappType: 2,
                        launchPage: "packageC/frogpro/storeManagement/storeManagement",
                        needLogin: 0,
                        success: function(e) {
                            wxfaceapp.postMsg({
                                targetAppid: a.data.front_accept_message_id,
                                content: JSON.stringify({
                                    money: t.data.money ? t.data.money : "",
                                    redirectType: "succeededPayment"
                                }),
                                success: function(t) {},
                                fail: function(t) {}
                            });
                        },
                        fail: function(t) {}
                    });
                }), wxfaceapp.onQueryPaymentFailedEvent(function(e) {
                    t.setData({
                        bulletShow: !1
                    }), wx.setNavigationBarColor({
                        backgroundColor: "#24ab35",
                        frontColor: "#000000",
                        animation: {
                            duration: 400,
                            timingFunc: "easeIn"
                        }
                    }), t.data.checkLogin ? (wx.showToast({
                        title: "等待后屏小程序登录",
                        icon: "none",
                        duration: 2e3
                    }), wxfaceapp.launchMp({
                        appId: a.data.front_accept_message_id,
                        hostAppId: a.data.front_mini_programs_id,
                        miniappType: 2,
                        launchPage: "packageC/frogpro/backScreenlogin/backScreenlogin",
                        needLogin: 0,
                        success: function(t) {
                            wxfaceapp.postMsg({
                                targetAppid: a.data.front_accept_message_id,
                                content: JSON.stringify({
                                    redirectType: "login"
                                }),
                                success: function(t) {},
                                fail: function(t) {}
                            });
                        },
                        fail: function(t) {}
                    }), t.data.checkLogin = !1) : wxfaceapp.launchMp({
                        appId: a.data.front_accept_message_id,
                        hostAppId: a.data.front_mini_programs_id,
                        miniappType: 2,
                        launchPage: "packageC/frogpro/storeManagement/storeManagement",
                        needLogin: 0,
                        success: function(e) {
                            wxfaceapp.postMsg({
                                targetAppid: a.data.front_accept_message_id,
                                content: JSON.stringify({
                                    money: t.data.money ? t.data.money : "",
                                    redirectType: "lose"
                                }),
                                success: function(t) {},
                                fail: function(t) {}
                            });
                        },
                        fail: function(t) {
                            wx.showToast({
                                title: t.reply,
                                icon: "none",
                                duration: 2e3
                            });
                        }
                    });
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: t.reply,
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    getQAdvertising: function() {
        var t = this;
        wx.request({
            url: t.getNetAddresss("plugin.advert-market.api.advert.getPutAdverts"),
            data: {
                store_id: {
                    id: t.data.store_id
                }
            },
            header: {
                "Content-Type": "application/json",
                Accept: "application/json",
                Cookie: "PHPSESSID=" + t.data.wx_token
            },
            method: "post",
            success: function(a) {
                var e = a.data;
                1 == e.result ? t.setData({
                    wait_adverts: e.data.wait_adverts
                }) : wx.showToast({
                    title: e,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: t,
                    icon: "none",
                    duration: 1500
                });
            }
        });
    },
    recordBtn: function(a) {
        var e = a.detail.current, o = [];
        try {
            var n = wx.getStorageSync("viewLog");
            n && (o = n);
        } catch (a) {
            console.log(a);
        }
        if (this.data.wait_adverts.lenght > 0) {
            var i = this.data.wait_adverts[e].id, s = this.data.wait_adverts[e].await_amount, c = 1;
            this.data.wait_adverts[e].view_count ? (this.setData(t({}, "wait_adverts[" + e + "].view_count", this.data.wait_adverts[e].view_count + 1)), 
            c = this.data.wait_adverts[e].view_count + 1) : (this.setData(t({}, "wait_adverts[" + e + "].view_count", 1)), 
            c = 1), o[e] = [ {
                advert_id: i,
                view_count: c,
                advert_unit: s
            } ];
            try {
                wx.setStorageSync("viewLog", o);
            } catch (a) {
                console.log(a);
            }
        }
    },
    uploadRecords: function() {
        var t = [];
        try {
            var a = wx.getStorageSync("viewLog");
            a && (t = a);
        } catch (t) {
            console.log(t);
        }
        if (t.length > 0) {
            for (var e = 0; e < t.length; e++) wx.request({
                url: this.getNetAddresss("plugin.advert-market.api.advert.getPutAdverts"),
                data: {
                    viewLog: {
                        store_id: this.data.store_id,
                        advert_id: t[e].advert_id,
                        advert_code: 1,
                        view_count: t[e].view_count,
                        unit_code: 2,
                        advert_unit: advert_unit
                    }
                },
                header: {
                    "Content-Type": "application/json",
                    Accept: "application/json",
                    Cookie: "PHPSESSID=" + this.data.wx_token
                },
                method: "post",
                success: function(t) {
                    var a = t.data;
                    0 == a.result && wx.showToast({
                        title: a,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    wx.showToast({
                        title: t,
                        icon: "none",
                        duration: 1500
                    });
                }
            });
            try {
                wx.setStorageSync("viewLog", []);
            } catch (t) {
                console.log(t);
            }
            this.getQAdvertising();
        } else this.getQAdvertising();
    }
});